package com.ruoyi.psi.purchase.storage.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDomain;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDto;
import com.ruoyi.psi.base.warehouse.mapper.WarehouseMapper;
import com.ruoyi.psi.purchase.storage.domain.MaterialAcceptanceRecord;
import com.ruoyi.psi.purchase.storage.domain.PurchaseStorage;
import com.ruoyi.psi.purchase.storage.domain.PurchaseStorageList;
import com.ruoyi.psi.purchase.storage.mapper.PurchaseStorageMapper;
import com.ruoyi.psi.quality.report.mapper.InspectReportMapper;
import com.ruoyi.utils.ID.IdUtil;
import com.ruoyi.utils.UtilsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 采购订单入库
 * 业务执行层
 */
@Service
public class PurchaseStorageServiceImpl implements IPurchaseStorageService {

    @Autowired
    private PurchaseStorageMapper purchaseStorageMapper;
    @Autowired
    private InspectReportMapper inspectReportMapper;
    @Autowired
    private WarehouseMapper warehouseMapper; //仓库
    @Autowired
    private IdUtil idUtil;

    /**
     * 采购订单入库查询
     *
     * @param purchaseStorage
     * @return
     */
    @Override
    public List<PurchaseStorage> selectPurchaseStorageList(PurchaseStorage purchaseStorage) {
        List<PurchaseStorage> purchaseStorages = purchaseStorageMapper.selectPurchaseStorageList(purchaseStorage);
        for (int i = 0; i < purchaseStorages.size(); i++) {
            if (StringUtils.isNotNull(purchaseStorageMapper.selectPurchaseStorageListListById(purchaseStorages.get(i).getPurchaseStorageId()))) {
                //存在，则存入采购订单入库对应产品信息
                purchaseStorages.get(i).setPurchaseStorageLists(purchaseStorageMapper.selectPurchaseStorageListListById(purchaseStorages.get(i).getPurchaseStorageId()));

//                // 判断对应产品是否质检
//                for (int j = 0; j < purchaseStorages.get(i).getPurchaseStorageLists().size(); j++) {
//                    if (StringUtils.isNull(inspectReportMapper.checkBySourceId(purchaseStorages.get(i).getPurchaseStorageId()))) {
//                        purchaseStorages.get(i).getPurchaseStorageLists().get(j).setAcceptancePhase("未检");
//                    } else {
//                        purchaseStorages.get(i).getPurchaseStorageLists().get(j).setAcceptancePhase("已检");
//                    }
//                }
            }
        }
        return purchaseStorages;
    }

    /**
     * 按编号查询
     *
     * @param purchaseStorageId 采购订单入库编号
     * @return
     */
    @Override
    public PurchaseStorage selectPurchaseStorageById(String purchaseStorageId) {
        return purchaseStorageMapper.selectPurchaseStorageById(purchaseStorageId);
    }

    /**
     * 采购订单入库新增
     *
     * @param purchaseStorage 采购入库单据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPurchaseStorage(PurchaseStorage purchaseStorage) throws Exception {
        if (StringUtils.isNotNull(purchaseStorageMapper.selectPurchaseStorageById(purchaseStorage.getPurchaseStorageId()))) {
            //存在
            return 0;
        }
        //不存在
        String purchaseStorageId = idUtil.createPurchaseStorageId();//自动生成编号
        purchaseStorage.setPurchaseStorageId(purchaseStorageId);//主表添加编号
        purchaseStorageMapper.insertPurchaseStorage(purchaseStorage);//采购订单入库据
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(purchaseStorage.getWarehouseId()); // 查询该仓库的产品列表
        List<PurchaseStorageList> purchaseStorageLists = purchaseStorage.getPurchaseStorageLists();//采购订单入库产品信息
        for (int i = 0; i < purchaseStorageLists.size(); i++) {
            purchaseStorageLists.get(i).setPurchaseStorageId(purchaseStorageId);//子表添加编号
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain(); // 仓库子表的对象
            warehouseListDomain.setWarehouseId(purchaseStorage.getWarehouseId());
            warehouseListDomain.setProduceId(purchaseStorageLists.get(i).getProduceId());
            boolean isExistFlag = false;
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (purchaseStorageLists.get(i).getProduceId().equals(s.getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品入库的数量
                    warehouseListDomain.setProductNumber(s.getProductNumber() + purchaseStorageLists.get(i).getPurchaseStorageNumber());
                    isExistFlag = true;
                    break; //终止循环
                }
            }
            if (isExistFlag) {
                // 该产品存在，更新库存
                warehouseMapper.updateWarehouseList(warehouseListDomain);
            } else {
                // 该产品不存在，在库存中新增产品
                warehouseListDomain.setProductNumber(purchaseStorageLists.get(i).getPurchaseStorageNumber());
                warehouseMapper.insertWarehouseList(warehouseListDomain);
            }
            purchaseStorageMapper.insertPurchaseStorageList(purchaseStorageLists.get(i));//采购订单入库产品信息
        }
        return 1;
    }

    /**
     * 采购订单入库更新
     *
     * @param purchaseStorage 采购入库单据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePurchaseStorage(PurchaseStorage purchaseStorage) throws Exception {
        if (!StringUtils.isNotNull(purchaseStorageMapper.selectPurchaseStorageById(purchaseStorage.getPurchaseStorageId()))) {
            //不存在
            return 0;
        }
        //存在采购订单入库单据
        purchaseStorageMapper.updatePurchaseStorage(purchaseStorage);//采购订单入库单据
        List<PurchaseStorageList> purchaseStorageListOld = purchaseStorageMapper.selectPurchaseStorageListListById(purchaseStorage.getPurchaseStorageId()); //原数据库中的子表数据
        List<PurchaseStorageList> purchaseStorageListNew = purchaseStorage.getPurchaseStorageLists(); //新修改的子表数据
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(purchaseStorage.getWarehouseId()); // 查询该仓库的产品列表
        for (int i = 0; i < purchaseStorageListNew.size(); i++) {
            // 查询子表中指定产品的对象
            PurchaseStorageList purchaseStorageList = purchaseStorageMapper.selectPurchaseStorageListById(purchaseStorage.getPurchaseStorageId(), purchaseStorageListNew.get(i).getProduceId());
            if (StringUtils.isNotNull(purchaseStorageList)) {
                for (int j = 0; j < purchaseStorageListOld.size(); j++) {
                    if (purchaseStorageListOld.get(j).getProduceId().equals(purchaseStorageList.getProduceId())) {
                        purchaseStorageListOld.remove(j);
                        break;
                    }
                }
                //存在采购订单入库详细
                //更新
                // 新建仓库子表的对象
                WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
                warehouseListDomain.setWarehouseId(purchaseStorage.getWarehouseId());
                warehouseListDomain.setProduceId(purchaseStorageListNew.get(i).getProduceId());
                for (WarehouseListDto s : warehouseListDtos) {
                    // 遍历仓库
                    if (purchaseStorageListNew.get(i).getProduceId().equals(s.getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品入库时增加的数量 + 新修改的入库数量
                        warehouseListDomain.setProductNumber(s.getProductNumber() - purchaseStorageList.getPurchaseStorageNumber() + purchaseStorageListNew.get(i).getPurchaseStorageNumber());
                        break; //终止循环
                    }
                }
                warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
                purchaseStorageMapper.updatePurchaseStorageList(purchaseStorageListNew.get(i));
            } else {
                // 子表中不存在该产品，新增产品和库存数，库存数 = 该产品新增时的入库数量
//                warehouseListDomain.setProductNumber(purchaseStorageListNew.get(i).getPurchaseStorageNumber());
//                warehouseMapper.insertWarehouseList(warehouseListDomain);
                purchaseStorageMapper.insertPurchaseStorageList(purchaseStorageListNew.get(i));
            }
        }
        // 说明有删除的对象
        for (int i = 0; i < purchaseStorageListOld.size(); i++) {
            // 新建仓库子表的对象
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(purchaseStorage.getWarehouseId());
            warehouseListDomain.setProduceId(purchaseStorageListOld.get(i).getProduceId());
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (purchaseStorageListOld.get(i).getProduceId().equals(s.getProduceId())) {
                    // 删除时，产品库存数 = 原有库存 - 该产品入库时的库存数
                    warehouseListDomain.setProductNumber(s.getProductNumber() - purchaseStorageListOld.get(i).getPurchaseStorageNumber());
                    break; //终止循环
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
            //最后将要删除的子标记录删除
            purchaseStorageMapper.deletePurchaseStorageListById(purchaseStorageListOld.get(i).getPurchaseStorageId(), purchaseStorageListOld.get(i).getProduceId());
        }
        return 1;
    }

    /**
     * 更新采购订单入库删除标志位
     *
     * @param purchaseStorage 采购入库单据
     * @return
     */
    @Override
    public int updatePurchaseStorageDeleteFlag(PurchaseStorage purchaseStorage) {
        if (!StringUtils.isNotNull(purchaseStorageMapper.selectPurchaseStorageById(purchaseStorage.getPurchaseStorageId()))) {
            //不存在
            return 0;
        }
        List<PurchaseStorageList> purchaseStorageLists = purchaseStorage.getPurchaseStorageLists(); // 被删除记录的所有子表记录
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(purchaseStorage.getWarehouseId()); // 查询该仓库的产品列表
        for (int i = 0; i < purchaseStorageLists.size(); i++) {
            // 新建仓库子表的对象
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(purchaseStorage.getWarehouseId());
            warehouseListDomain.setProduceId(purchaseStorageLists.get(i).getProduceId());
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (purchaseStorageLists.get(i).getProduceId().equals(s.getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品入库时增加的数量
                    warehouseListDomain.setProductNumber(s.getProductNumber() - purchaseStorageLists.get(i).getPurchaseStorageNumber());
                    break; //终止循环
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
        }
        return purchaseStorageMapper.updatePurchaseStorageDeleteFlag(purchaseStorage);
    }

    /**
     * 采购订单入库详细查询
     *
     * @param purchaseStorageList 采购入库产品信息
     * @return
     */
    @Override
    public List<PurchaseStorageList> selectPurchaseStorageListList(PurchaseStorageList purchaseStorageList) {
        return purchaseStorageMapper.selectPurchaseStorageListList(purchaseStorageList);
    }

    /**
     * 查询采购订单入库对应的所有产品详细
     *
     * @param purchaseStorageId 采购订单入库编号
     * @return
     */
    @Override
    public List<PurchaseStorageList> selectPurchaseStorageListListById(String purchaseStorageId) {
        return purchaseStorageMapper.selectPurchaseStorageListListById(purchaseStorageId);
    }

    /**
     * 采购入库新增时查询采购订单对应信息
     *
     * @param purchaseOrderId
     * @return
     */
    @Override
    public List<PurchaseStorageList> selectPurchaseOrderListForStorage(String purchaseOrderId) {
        List<PurchaseStorageList> purchaseStorageLists = purchaseStorageMapper.selectPurchaseOrderListForStorage(purchaseOrderId);
        for (int i = 0; i < purchaseStorageLists.size(); ) {
            if (purchaseStorageLists.get(i).getPurchaseStorageNumber() <= 0) {
                purchaseStorageLists.remove(i);
            } else {
                i++;
            }
        }
        return purchaseStorageLists;
    }

    /**
     * 查询采购订单入库对应的指定产品详细
     *
     * @param purchaseStorageId 采购订单入库编号
     * @param produceId         产品编号
     * @return
     */
    @Override
    public PurchaseStorageList selectPurchaseStorageListById(String purchaseStorageId, String produceId) {
        return purchaseStorageMapper.selectPurchaseStorageListById(purchaseStorageId, produceId);
    }

//    /**
//     * 物料验收看板显示类
//     *
//     * @return
//     */
//    @Override
//    public List<MaterialAcceptanceRecord> selectProduceForKanBan() {
//        List<MaterialAcceptanceRecord> MARList = purchaseStorageMapper.selectProduceForKanBan();
//        for (MaterialAcceptanceRecord m : MARList) {
//            if (StringUtils.isNull(inspectReportMapper.checkBySourceId(m.getPurchaseStorageId()))) {
//                m.setAcceptancePhase("未检");
//            } else {
//                m.setAcceptancePhase("已检");
//            }
//        }
//        return MARList;
//    }
}
